from datetime import datetime, timedelta


local_background_project_path = '/home/Tang/work/a-ops'
local_front_project_path = '/home/Tang/work/a-ops-ui'

server_background_project_path = '/'
server_front_project_path = '/data/a-ops-ui'

# 设置服务器连接信息
server_host = 'touni.top'
server_port = 19016
server_username = 'Tang'

# 设置时间范围（最近3小时）
time_threshold = datetime.now() - timedelta(hours=1)

# 连接到服务器
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=server_host, port=server_port, username=server_username)


def command(cmd):
    stdin, stdout, stderr = ssh_client.exec_command(cmd)
    err = stderr.read().decode()
    if err:
        return False, err
    return True, stdout.read().decode()


# 更新项目
_, result = command(f"cd {server_background_project_path} && git add . &&  git reset --hard && git pull")
print(result)
# if status:
#     print('更新后端项目文件完成')
# else:
#     print('更新后端项目文件失败 \n', result)
#     exit(1)

_, result = command(f"cd {server_front_project_path} && git add . &&  git reset --hard && git pull")
print(result)
# if status:
#     print('更新前端项目文件完成')
# else:
#     print('更新前端项目文件失败 \n', result)
#     exit(1)


_, result = command(f"cd {server_background_project_path} && source /etc/profile && bash restart.sh")
print(result)
# if status:
#     print('后端项目启动完成')
# else:
#     print('后端项目启动失败 \n', result)
#     exit(1)

_, result = command(f"cd {server_front_project_path} && source /etc/profile && vite build")
print(result)
# if status:
#     print('前端项目启动完成')
# else:
#     print('前端项目启动失败 \n', result)
#     exit(1)


ssh_client.close()

